package RiverCrossingMinigame;

// Some code was adapted from the Virginia CS homework assignments and 
// tutorials/examples/explanations found here http://www.cs.virginia.edu/cs201j/problem-sets/ps5/

import java.awt.Color;


/**
 * This class represents a floating piece that moves through a lane in the river crossing minigame.
 * In this implementation, the pieces are drawn as logs. 
 * 
 * 
 * @author Ray DeCuir
 * @version 1.0
 *
 */
public class FloatingPiece extends Movable{

	// the direction of the piece, either left or right
	private int direction;

	
	/**
	 * Constructor for the floating piece that sets the direction of movement for the piece
	 * 
	 * @param direction The direction for the piece
	 */
	public FloatingPiece(int direction) {
		this.direction = direction;
	}

	
	/**
	 * Method that moves the floating piece in the lane, checks to see if the piece collides with the player
	 * if so, the game exits
	 * 
	 */
	public void executeTurn() throws RuntimeException
	{	
		int oldy = getY();
		int newx = getX();
		
		if(direction > 0){
			if(newx == 19)
			{
				grid.removeObjectAt(getX(), getY());
				newx = 0;
			}
			else
				newx += direction;
		}
		else{
			if(newx == 0)
			{
				grid.removeObjectAt(getX(), getY());
				newx = 19; 
			}
			else
				newx += direction;
		}
	
		// check to see if the log has collided with the player
		synchronized(getGrid()){
			try{
				setLocation(newx, oldy);
			}
			catch(RuntimeException e){
			//	GamePanel.player.lose();
				GamePanel.crash = true;
				try {
					java.awt.Robot r = new java.awt.Robot();
					r.keyPress('a');
					r.keyRelease('a');
				} catch(java.awt.AWTException ex) {
					
				}
			} 
			
			grid.repaint();
		}
	}

	/**
	 * Gets the color for this floating piece, in this case red
	 * 
	 * @return The color of the piece
	 */
	public Color getColor() {
		return Color.red;
	}
}

